package com.darren.euc.action;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.Province;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.entity.Space;
import com.darren.euc.entity.SpaceStatus;
import com.darren.euc.entity.User;
import com.darren.euc.service.SpaceService;
import com.darren.euc.util.PropertyHelper;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/space")
public class SpaceAction {
	
	private static final Logger log = Logger.getLogger(RoleAction.class);
	
	@Autowired
    private SpaceService spaceService;
	
	@ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public List<Space> getAllFunctions(HttpServletRequest request,HttpServletResponse response) {
    	List<Space> spaces = spaceService.getAll();    	
        return spaces;
    }
	
	@ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public ResponseJSON querySpace(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = spaceService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	@ResponseBody
    @RequestMapping(value = "/find",method = RequestMethod.POST)
    public List<Space> querySpaceByCondition(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		List<Space> spaces = spaceService.queryByCondition(requestJson.getJSONObject("data"));
		return spaces;
    }
	
	@ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseJSON saveSpace(@RequestBody Space space,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			space.setCreateUser(user);
			space.setCreateTime(new Date());
			space = spaceService.save(space);
			responseJson.setStatus(0);
			responseJson.setMessage("添加成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("添加失败！");
		}
		responseJson.setData(space);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
    }
	
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseJSON updateSpace(@RequestBody Space space, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			if(user != null){
				space.setCreateUser(user);
			}			
			space.setCreateTime(new Date());
			space = spaceService.update(space);
			responseJson.setStatus(0);
			responseJson.setMessage("保存成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("保存失败！");
		}
		responseJson.setData(space);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
	
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseJSON deleteData(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {
    	ResponseJSON responseJson = new ResponseJSON();
    	JSONObject jsonData = new JSONObject();
    	responseJson.setData(jsonData);    	
    	try{
    		Integer id = Integer.parseInt(requestJson.getString("id"));
    		spaceService.deleteById(id);
        	responseJson.setStatus(0);
    		responseJson.setMessage("删除成功！");
    	}catch(Exception ex){
    		log.info(ex.getMessage(),ex);
        	responseJson.setStatus(-1);
    		responseJson.setMessage("删除失败！");
    	}
    	System.out.println("==============================================================");
    	System.out.println(requestJson.toString());
    	System.out.println(responseJson.toString());
    	System.out.println("==============================================================");
        return responseJson;
    }

	
	@ResponseBody
    @RequestMapping(value = "/province",method = RequestMethod.POST)
    public List<Province> getAllProvinces(HttpServletRequest request,HttpServletResponse response) {
    	List<Province> provinces = spaceService.getAllProvinces();   	
        return provinces;
    }
	
	@ResponseBody
    @RequestMapping(value = "/status",method = RequestMethod.POST)
    public List<SpaceStatus> getAllSpaceStatus(HttpServletRequest request,HttpServletResponse response) {
    	List<SpaceStatus> spaceStatus = spaceService.getAllSpaceStatus();
        return spaceStatus;
    }
	
	
}